package com.example.tellwin.view;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.common.Common;
import com.example.tellwin.utils.CommonUtils;
import com.example.tellwin.utils.FileUtil;
import com.example.tellwin.utils.ToastUtil;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.example.tellwin.common.Common.PhotoAlbumRequestCode;
import static com.example.tellwin.common.Common.TakePhotoRequestCode;

/**
 * @author : LinZePeng
 * @date : 2020/11/9
 * @Describe ：
 **/
public class TakePhotoDialog extends Dialog {

    @BindView(R.id.take_photo_btn)
    Button takePhotoBtn;
    @BindView(R.id.photo_album_btn)
    Button photoAlbumBtn;
    @BindView(R.id.cancel_btn)
    Button cancelBtn;

    String takePhotoPath;

    Activity activity;

    public TakePhotoDialog(@NonNull Activity activity) {
        super(activity);
        this.activity = activity;
    }

    public String getTakePhotoPath() {
        return takePhotoPath;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_take_photo);
        ButterKnife.bind(this);
        Window window = this.getWindow();
        //设置位置在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置弹入弹出动画
        window.setWindowAnimations(R.style.BottomDialog_Animation);
        //设置为全屏dialog
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

    }

    @OnClick({R.id.take_photo_btn, R.id.photo_album_btn, R.id.cancel_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.take_photo_btn:

                if(!CommonUtils.requestPermissions(MainApplication.getCurActivity())){
                    return;
                }

                Intent getImageByCamera = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(Common.PhotoPath);
                if (!f.exists()) {
                    boolean is = f.mkdirs();
                    try {
                        f.createNewFile();
                    } catch (IOException e) {
                        LogUtils.e("文件创建失败");
                        FileUtil.getOrCreateFile(Common.PhotoPath);
                        e.printStackTrace();
                        return;
                    }
                }
                String fileName = System.currentTimeMillis() + ".jpg";
                File file = new File(Common.PhotoPath, fileName);
                takePhotoPath = file.getPath();
                LogUtils.e(takePhotoPath);
                Uri uri;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", file);
                } else {
                    uri = Uri.fromFile(file);
                }
                getImageByCamera.putExtra(MediaStore.Images.Media.ORIENTATION,
                        0);
                getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                activity.startActivityForResult(getImageByCamera, TakePhotoRequestCode);
                dismiss();
                break;
            case R.id.photo_album_btn:
                if(!CommonUtils.requestPermissions(MainApplication.getCurActivity())){
                    return;
                }
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                activity.startActivityForResult(intent, PhotoAlbumRequestCode);
                dismiss();
                break;
            case R.id.cancel_btn:
                dismiss();
                break;
        }
    }
}
